// Main.java
package org.example;

import org.example.model.*;
import java.util.*;
import java.math.BigDecimal;

public class Main {
    private static List<Product> productList = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("=== 商品管理系统 ===");

        while (true) {
            printMenu();
            int choice = getIntInput("请选择操作: ");

            switch (choice) {
                case 1:
                    addProduct();
                    break;
                case 2:
                    queryProduct();
                    break;
                case 3:
                    adjustPrice();
                    break;
                case 4:
                    displayAllProducts();
                    break;
                case 0:
                    System.out.println("感谢使用，再见！");
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private static void printMenu() {
        System.out.println("\n1. 添加商品");
        System.out.println("2. 查询商品");
        System.out.println("3. 调整价格");
        System.out.println("4. 显示所有商品");
        System.out.println("0. 退出系统");
    }

    private static void addProduct() {
        System.out.println("\n=== 添加商品 ===");
        System.out.println("1. 食品");
        System.out.println("2. 日用品");
        int type = getIntInput("选择商品类型: ");

        String id = getStringInput("输入商品ID: ");
        String name = getStringInput("输入商品名称: ");
        BigDecimal price = getBigDecimalInput("输入商品价格: ");
        int quantity = getIntInput("输入库存数量: ");

        try {
            if (type == 1) {
                String expiryDate = getStringInput("输入保质期: ");
                String manufacturer = getStringInput("输入生产商: ");
                productList.add(new FoodProduct(id, name, price, quantity, expiryDate, manufacturer));
            } else if (type == 2) {
                String material = getStringInput("输入材质: ");
                boolean recyclable = getIntInput("是否可回收(1-是/0-否): ") == 1;
                productList.add(new DailyProduct(id, name, price, quantity, material, recyclable));
            }
            System.out.println("商品添加成功！");
        } catch (Exception e) {
            System.out.println("添加商品失败: " + e.getMessage());
        }
    }

    private static void queryProduct() {
        String id = getStringInput("\n输入要查询的商品ID: ");
        Product product = findProductById(id);

        if (product != null) {
            product.displayInfo();
        } else {
            System.out.println("未找到该商品！");
        }
    }

    private static void adjustPrice() {
        String id = getStringInput("\n输入要调整价格的商品ID: ");
        Product product = findProductById(id);

        if (product == null) {
            System.out.println("未找到该商品！");
            return;
        }

        BigDecimal newPrice = getBigDecimalInput("输入新价格: ");

        try {
            product.setPrice(newPrice);
            System.out.println("价格调整成功！");
        } catch (Exception e) {
            System.out.println("价格调整失败: " + e.getMessage());
        }
    }

    private static void displayAllProducts() {
        if (productList.isEmpty()) {
            System.out.println("\n当前没有商品记录！");
            return;
        }

        System.out.println("\n=== 所有商品信息 ===");
        productList.forEach(Product::displayInfo);
    }

    private static Product findProductById(String id) {
        return productList.stream()
                .filter(p -> p.getId().equals(id))
                .findFirst()
                .orElse(null);
    }

    // 辅助方法
    private static String getStringInput(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine();
    }

    private static int getIntInput(String prompt) {
        while (true) {
            try {
                System.out.print(prompt);
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字！");
            }
        }
    }

    private static BigDecimal getBigDecimalInput(String prompt) {
        while (true) {
            try {
                System.out.print(prompt);
                return new BigDecimal(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的价格！");
            }
        }
    }
}